package sailing.ui.model;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

import cjlite.logger.Logger;

/**
 * @author
 * 
 */
public class EditBtnCellEditor extends AbstractCellEditor implements TableCellEditor {

	private static Logger logger = Logger.thisClass();

	private int row;
	private int column;
	JButton editBtn, deleteBtn;
	private Object value;
	private static Insets margeInsets = new Insets(0, 4, 0, 4);
	private ActionListener editAL;
	private ActionListener deleteAL;

	/**
	 * @param cbcb
	 * 
	 */
	public EditBtnCellEditor(String editTxt, String deleteTxt) {
		editBtn = new JButton(editTxt);
		editBtn.setMargin(margeInsets);

		deleteBtn = new JButton(deleteTxt);
		deleteBtn.setMargin(margeInsets);
	}


	@Override
	public Object getCellEditorValue() {
		return value;
	}


	public boolean stopCellEditing() {
		return super.stopCellEditing();
	}


	protected void fireEditingStopped() {
		super.fireEditingStopped();
	}


	@Override
	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		this.row = row;
		this.column = column;
		this.value = value;
		logger.trace("row[{0}] column[{1}] is clicked",row,column);
		JPanel cellP = new JPanel();
		cellP.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.CENTER;

		Box box = Box.createHorizontalBox();
		box.add(editBtn);
		box.add(Box.createHorizontalStrut(10));
		box.add(deleteBtn);
		cellP.add(box, c);
		cellP.setOpaque(false);

		box.setBackground(Color.blue);
		cellP.add(box, c);
		cellP.setBackground(Color.blue);
		if (isSelected) {
			cellP.setBackground(table.getSelectionBackground());
		} else {
			cellP.setBackground(table.getBackground());
		}
		cellP.setBackground(table.getSelectionBackground());
		cellP.setBackground(Color.blue);

		return cellP;
	}


	public void addBtn1Listener(ActionListener _editAL) {
		this.editAL = _editAL;
		if (editBtn != null) {
			editBtn.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					logger.trace("Edit btn is clicked");
					ActionEvent ae = new ActionEvent(EditBtnCellEditor.this, e.getID(), e.getActionCommand());
					editAL.actionPerformed(ae);
				}
			});
		}
	}


	public void addBtn2Listener(ActionListener _deleteAL) {
		this.deleteAL = _deleteAL;
		if (deleteBtn != null) {
			deleteBtn.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					logger.trace("delete btn is clicked");
					ActionEvent ae = new ActionEvent(EditBtnCellEditor.this, e.getID(), e.getActionCommand());
					deleteAL.actionPerformed(ae);
				}
			});
		}
	}
}